<template>
  <div class="renlian">
    <div style="position: relative;width:100%;height:50px;">
      <van-nav-bar
        fixed
        title="人脸认证"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div class="top-recognition">
      <div class="congratulations">恭喜你，仅差最后一步</div>
      <div class="hint">快来刷个脸，确保本人申请哦~</div>
      <img class="people" src="../../assets/people.png" alt="">
      <van-button class="shualian" round  size="normal"  @click="faceAuthClick">开始刷脸</van-button>

    </div>
    <div>
      <van-checkbox size="{14}" class="bottom-agreement" checked-color="#FF8024" v-model="checked">同意<a href="http://h5.daxiongdaikuan.com/#/PlatformServices">《大熊贷款平台服务协议》</a></van-checkbox>
    </div>
  </div>
</template>

<script>
import { Checkbox, CheckboxGroup, Button, Toast, Dialog,NavBar  } from 'vant';

// const AES = require("crypto-js/aes");
// const CryptoJS  = require("crypto-js");
export default {
  components:{
    [Checkbox.name]:Checkbox,
    [CheckboxGroup.name]:CheckboxGroup,
    [Button.name]:Button,
    [Toast.name]:Toast,
    [Dialog.name]:Dialog,
    [NavBar.name]:NavBar,
  },
  data(){
    return{
      checked: true,
      natureFile:'',//活体图片
      confidenceLevel:'',//识别分值等级
      idCards:'',//id信息
      token:'',
      step:0
    }
  },
  mounted(){
    _hmt.push(['_trackPageview', '/renlian']);
    this.$util.goBack(Dialog);

    Toast.loading({duration:1000,mask: false,message: '加载中...'});
    let _this =this;
    // 这里是回调
    // this.natureFile = ''
    window.resultFace=(state,result,img)=>{
      // alert(state+ '，0是成功，1是失败')
      // alert(result)
      if(state==0){
        //活体图片  参数 编码
        let data = JSON.parse(result);
        if(data.result.score==0){
          Toast('请身份证本人重新识别')
        }else{
          this.natureFile = 'data:image/png;base64,'+img;
          this.confidenceLevel = data.result.score;
          this.faceAuth();
        }
      }else{
        Toast('认证失败，请重新操作')
      }

    }
    this.$util.getUser().then(token=>{
       _this.token=token;
      console.log(token)
      if(token!=-1){
        this.idCard();
        this.borrowBeforeSchedule();
      }

    })
  },
  methods:{
    onClickLeft(){
      this.$util.url('/HomeBefore','首页',Dialog);
    },
    idCard(){
      let _this =this;
      this.$api.get('/user/v1/idCard',{},s=>{
          console.log(s)
          _this.idCards = s.module;
        // this.generateOperatorAuthOrderNo();
      },f=>{
        Toast(f.errorMessage);
      },{
        'Content-Type': 'application/json',
        'Accept': "*/*",
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })

    },
    // 进度查询
      borrowBeforeSchedule(){
        let _this = this;
        this.$api.get('/borrowBefore/v1/borrowBeforeSchedule?bankrollCode='+this.$util.bankrollCode,{},s=>{
          _this.step = s.module.step
        },f=>{
            if(f.resultCode=='10003'||f.resultCode=='10000'||f.resultCode=='10001'||f.resultCode=='10002'){
              _this.$util.url('/HomeBefore','首页')
            }else{
              Toast(f.errorMessage)
            }

        },{
          'Accept': 'application/json',
          'channelCode':this.$util.channelCode,
          'version':this.$util.version,
          'token':this.token
        })
      },
    // 刷脸方法
    faceAuthClick(){
      if(this.step == 50){
        this.applyBorrowMoney();//获取订单号
        return false;
      }
      if(!this.checked){
        Toast('请先勾选协议，然后操作！');
        return false;
      }
      let name = this.idCards.name;
      let number = this.idCards.idCard;
      let frontFile = this.idCards.frontFile;
      // let frontFile = 'http://h5.daxiongdaikuan.com/2.png';
      // 调用安卓方法
      let userAgentObj = navigator.userAgent;
      if (userAgentObj.match(/Android/i)) {
          Toast.loading({
            duration: 3000,
            message: '请稍后...'
          });
         window.control.onFace(frontFile);
      } else if (userAgentObj.match(/iPhone/i)) {


      }


    },
    faceAuth(){
      let _this =this;
      // alert('提交人脸识别')
      this.$api.post('/borrowBefore/v1/faceAuth',{
          "bankrollCode": this.$util.bankrollCode,
          "confidenceLevel":this.confidenceLevel,
          "natureFile": this.natureFile
      },s=>{
          console.log(s)
          _this.step = 50;
        if(s.module.step ==50){
          _this.applyBorrowMoney();//获取订单号
        }else{
          Toast(s.module.step);
        }
        // this.generateOperatorAuthOrderNo();
      },f=>{
        if(f.resultCode=='20024'||f.resultCode=='10000'||f.resultCode=='10003'||f.resultCode=='10001'||f.resultCode=='10002'){
          this.$util.url('/HomeBefore','首页')
        }else{
          Toast(f.errorMessage);
        }
      },{
        'Content-Type': 'application/json',
        'Accept': "*/*",
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })
    },
    // //运营认证
    // generateOperatorAuthOrderNo(){
    //   this.$api.post('/borrowBefore/v1/generateOperatorAuthOrderNo?bankrollCode='+this.$util.bankrollCode,{},s=>{
    //     console.log(s)
    //     // if(s.module.step ==40){
    //     //   _this.$router.push({ path: '/FaceRecognition'})
    //     // }

    //   },f=>{
    //     Toast(f.errorMessage);
    //   },{
    //     'Content-Type': 'application/json',
    //     'Accept': "*/*",
    //     'channelCode':this.$util.channelCode,
    //     'version':this.$util.version,
    //     'token':this.token
    //   })

    // },
    //进件获取订单号
    applyBorrowMoney(){
      // alert('开始进件')
      let _this = this;
       this.$api.post('/borrowBefore/v1/applyBorrowMoney?bankrollCode='+this.$util.bankrollCode,{},s=>{
        console.log(s)
        let orderNo = s.module.orderNo;
        _this.$util.h5Authenticate(orderNo);
        },f=>{
        if(f.resultCode=='20005'||f.resultCode=='10000'||f.resultCode=='10003'||f.resultCode=='10001'||f.resultCode=='10002'){
          // ，如果返回20005，则跳转到审核失败界面,剩余天数显示7天
          _this.$util.url('/HomeBefore','首页')
        }else if(f.resultCode=='20012'){
          Toast('获取三方认证失败，请重试！');

        }else if(f.resultCode=='20013'){
          Toast('审核失败');
          _this.$util.url('/HomeBefore','首页')
        }else{
          Toast(f.errorMessage);
        }
      },{
        'Content-Type': 'application/json',
        'Accept': "*/*",
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })
    },

  }
}
</script>

<style>
.bottom-agreement .van-icon{
  position:relative;
  top:2px;
  font-size: 12px;
  width: 13px;
  height: 13px;
  font: 12px/1 "vant-icon";
}
</style>

<style scoped lang="scss">
  /***
  * 产品主题颜色需求
  * #FF8024 按钮 突出文字 ico
  * #FFD500 B辅助色
  * #FFEC9C c辅助色
  * #1D0F00 重要文字
  * #41424E 副标题段落
  * #797A79 次要文字
  * #A9AAA9 不可点击
  * #E6E6E6 分割线 描边
  * #F6F7F6 背景
  * #6699CC 链接
  */
.renlian{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: center;
  width:100%;
  height:100%;
}
.top-recognition{
  width:100%;
  height:491px;
}
.shualian{
  width:80%;
  margin-top:60px;
  color:#fff;;
  background:#FF8024;
}
.congratulations{
  margin-top:44px;
  font-size:20px;
  line-height: 28px;
  color:#41424E;
}
.hint{
  margin-top:12px;
  font-size:16px;
  line-height: 22px;
  color:#797A79;
}
.people{
  width:180px;
  height:180px;
  margin-top:40px;
}
.bottom-agreement{
  margin-bottom:10px;
  font-size:12px;
  color:#41424E;

  a{
    font-size:12px;
    color:#6699CC;
  }
}
</style>
